package com.yanqu.road.server.pb;

import com.yanqu.road.entity.season.SeasonRewardConfig;
import com.yanqu.road.entity.season.SeasonRewardData;
import com.yanqu.road.entity.season.SeasonRewardKey;
import com.yanqu.road.pb.activity.SeasonRewardProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.Map;
import java.util.Optional;

public class SeasonRewardPb {

    public static SeasonRewardProto.SeasonRewardSyncConfigRespMsg.Builder parseSeasonRewardSyncConfigRespMsg(
            Map<Integer, Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>>> inTimeConfigMap, String language) {

        SeasonRewardProto.SeasonRewardSyncConfigRespMsg.Builder builder = SeasonRewardProto.SeasonRewardSyncConfigRespMsg.newBuilder();

        for (Map.Entry<Integer, Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>>> systemEntry : inTimeConfigMap.entrySet()) {
            // 系统ID
            int systemId = systemEntry.getKey();
            for (Map.Entry<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>> seasonEntry : systemEntry.getValue().entrySet()) {
                // 赛季ID
                int seasonId = seasonEntry.getKey();
                // 所有类型全部下发
                for (Map.Entry<Integer, Map<Integer, SeasonRewardConfig>> typeEntry : seasonEntry.getValue().entrySet()) {
                    int type = typeEntry.getKey();
                    builder.addList(parseSeasonRewardConfigMsg(language, systemId, seasonId, type, typeEntry.getValue()));
                }
            }
        }

        return builder;
    }

    private static SeasonRewardProto.SeasonRewardConfigMsg.Builder parseSeasonRewardConfigMsg(
            String language, int systemId, int seasonId, int type, Map<Integer, SeasonRewardConfig> idConfigMap) {

        SeasonRewardProto.SeasonRewardConfigMsg.Builder configBuilder = SeasonRewardProto.SeasonRewardConfigMsg.newBuilder();

        SeasonRewardProto.SeasonRewardKey.Builder keyBuilder = SeasonRewardProto.SeasonRewardKey.newBuilder()
                .setSystemId(systemId)
                .setSeasonId(seasonId)
                .setType(type);
        configBuilder.setKey(keyBuilder);

        for (SeasonRewardConfig config : idConfigMap.values()) {
            SeasonRewardProto.SeasonRewardConfigTempMsg.Builder tempMsg = SeasonRewardProto.SeasonRewardConfigTempMsg.newBuilder()
                    .setRewardId(config.getRewardId())
                    .setTitle(ServerLanguageMgr.getContent(config.getTitle(), language))
                    .setDesc(ServerLanguageMgr.getContent(config.getDesc(), language))
                    .setParam(config.getParam())
                    .setReward(config.getReward());
            configBuilder.addList(tempMsg);
        }

        return configBuilder;
    }

    public static SeasonRewardProto.SeasonRewardSyncDataRespMsg.Builder parseSeasonRewardSyncDataRespMsg(
            Map<Integer, Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>>> inTimeConfigMap,
            Map<Integer, Map<Integer, Map<Integer, Map<Integer, SeasonRewardData>>>> dataMap) {

        SeasonRewardProto.SeasonRewardSyncDataRespMsg.Builder builder = SeasonRewardProto.SeasonRewardSyncDataRespMsg.newBuilder();

        for (Map.Entry<Integer, Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>>> systemEntry : inTimeConfigMap.entrySet()) {
            // 系统ID
            int systemId = systemEntry.getKey();
            Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>> seasonConfigMap = systemEntry.getValue();
            Map<Integer, Map<Integer, Map<Integer, SeasonRewardData>>> seasonDataMap = dataMap.get(systemId);
            if (seasonDataMap == null || seasonDataMap.isEmpty()) {
                continue;
            }

            for (Map.Entry<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>> seasonEntry : seasonConfigMap.entrySet()) {
                // 赛季ID
                int seasonId = seasonEntry.getKey();

                Map<Integer, Map<Integer, SeasonRewardData>> typeDataMap = seasonDataMap.get(seasonId);
                if (typeDataMap == null || typeDataMap.isEmpty()) {
                    continue;
                }
                // 所有类型全部下发
                for (Map.Entry<Integer, Map<Integer, SeasonRewardData>> typeEntry : typeDataMap.entrySet()) {
                    int type = typeEntry.getKey();
                    Map<Integer, SeasonRewardData> idDataMap = typeEntry.getValue();
                    builder.addList(parseSeasonRewardDataTempMsg(systemId, seasonId, type, idDataMap));
                }
            }
        }
        return builder;
    }

    public static SeasonRewardProto.SeasonRewardSyncDataRespMsg.Builder parseSeasonRewardSyncDataRespMsg(
            Map<Integer, Map<Integer, Map<Integer, Map<Integer, SeasonRewardData>>>> dataMap) {

        SeasonRewardProto.SeasonRewardSyncDataRespMsg.Builder builder = SeasonRewardProto.SeasonRewardSyncDataRespMsg.newBuilder();

        for (Map<Integer, Map<Integer, Map<Integer, SeasonRewardData>>> value : dataMap.values()) {
            for (Map<Integer, Map<Integer, SeasonRewardData>> mapMap : value.values()) {
                for (Map<Integer, SeasonRewardData> map : mapMap.values()) {
                    Optional<SeasonRewardData> first = map.values().stream().findFirst();
                    if (!first.isPresent()) {
                        continue;
                    }
                    SeasonRewardData data = first.get();
                    builder.addList(parseSeasonRewardDataTempMsg(data.getSystemId(), data.getSeasonId(), data.getType(), map));
                }
            }
        }

        return builder;
    }


    private static SeasonRewardProto.SeasonRewardDataMsg.Builder parseSeasonRewardDataTempMsg(
            int systemId, int seasonId, int type, Map<Integer, SeasonRewardData> dataMap) {

        SeasonRewardProto.SeasonRewardDataMsg.Builder dataBuilder = SeasonRewardProto.SeasonRewardDataMsg.newBuilder();

        SeasonRewardProto.SeasonRewardKey.Builder keyBuilder = SeasonRewardProto.SeasonRewardKey.newBuilder()
                .setSystemId(systemId)
                .setSeasonId(seasonId)
                .setType(type);
        dataBuilder.setKey(keyBuilder);

        for (SeasonRewardData data : dataMap.values()) {
            SeasonRewardProto.SeasonRewardDataTempMsg.Builder tempMsg = SeasonRewardProto.SeasonRewardDataTempMsg.newBuilder()
                    .setRewardId(data.getRewardId())
                    .setValue(data.getValue())
                    .setGetReward(data.isGetReward());
            dataBuilder.addList(tempMsg);
        }

        return dataBuilder;
    }

    public static SeasonRewardKey parseSeasonRewardKey(SeasonRewardProto.SeasonRewardKey keyMsg) {
        SeasonRewardKey key = new SeasonRewardKey();
        key.setSystemId(keyMsg.getSystemId());
        key.setSeasonId(keyMsg.getSeasonId());
        key.setType(keyMsg.getType());
        return key;
    }
}
